package com.work.straighttalk.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;

import com.work.straighttalk.R;
import com.work.straighttalk.app.BaseActivity;
import com.work.straighttalk.presenter.SMSPImpl;
import com.work.straighttalk.presenter.i.SMSPI;
import com.work.straighttalk.ui.i.SMSVI;
import com.work.straighttalk.view.ActionProcessButton;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by dllo on 16/3/3.
 */
public class SMSActivity extends BaseActivity implements SMSVI {


    @Bind(R.id.login_input_phone_et)
    EditText loginInputPhoneEt;
    @Bind(R.id.login_input_code_et)
    EditText loginInputCodeEt;
    @Bind(R.id.login_commit_btn)
    ActionProcessButton loginCommitBtn;
    @Bind(R.id.login_request_code_btn)
    Button loginRequestCodeBtn;

    private SMSPI smspi;

    int i = 30;
    private String phoneNums, code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        ButterKnife.bind(this);
        smspi = new SMSPImpl(this);
        init();
    }

    @Override
    public void phoneNumError() {
        toast("手机号码格式错误");
    }

    @Override
    public void phoneNumNull() {
        toast("手机号码为空");
    }

    @Override
    public void passwordNumNull() {
        toast("验证码为空");
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toMainActivity() {
        toast("注册成功");
        loginCommitBtn.setProgress(100);
        startActivity(MainActivity.class, null, true);
    }

    @Override
    public void showErrorToast(BmobException e) {
        toast(e.getMessage() + "(" + e.getErrorCode() + ")");
    }

    @Override
    public void showBtnCountdown() {
        new CountDownTimer(30 * 1000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                i--;
                loginRequestCodeBtn.setText("重新发送" + "(" + i + ")");
                loginRequestCodeBtn.setClickable(false);
            }

            @Override
            public void onFinish() {
                i = 30;
                loginRequestCodeBtn.setText("获取验证码");
                loginRequestCodeBtn.setClickable(true);
            }
        }.start();
    }

    @Override
    public void codeIsOK() {
        toast("验证码已发送");
    }


    /**
     * 初始化短信sdk
     */
    private void init() {
        //初始化mob
        SMSSDK.initSDK(this, "ffba1548c04e", "2c72883a953878607cb77433025d8de0");
        EventHandler eventHandler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
            }
        };
        //注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);
    }

    @OnClick(R.id.login_request_code_btn)
    protected void sendMessages() {
        phoneNums = loginInputPhoneEt.getText().toString();
        SMSSDK.getVerificationCode("86", phoneNums);
        smspi.validateSMS(phoneNums);
    }

    @OnClick(R.id.login_commit_btn)
    protected void getCode() {
        loginCommitBtn.setProgress(50);
        phoneNums = loginInputPhoneEt.getText().toString();
        code = loginInputCodeEt.getText().toString();
        smspi.validateSMS(phoneNums, code);
    }


    @Override
    protected void onDestroy() {
        SMSSDK.unregisterAllEventHandler();
        smspi.onDestroy();
        super.onDestroy();
    }
}
